import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  UseGuards,
  HttpCode,
  Query,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AlbumService } from './album.service';
import {
  CreateAlbumDto,
  UpdateAlbumDto,
  AlbumResponseDto,
  AlbumsPaginationQueryDto,
} from './album.dto';
import { PaginationResponseDto } from '../song/song.dto';
import { Song } from '../song/song.entity';

@ApiTags('专辑')
@Controller('albums')
export class AlbumController {
  constructor(private readonly albumService: AlbumService) {}

  @ApiOperation({ summary: '创建新专辑' })
  @ApiResponse({ status: 201, type: AlbumResponseDto })
  @ApiResponse({ status: 404, description: '歌手不存在' })
  @ApiResponse({ status: 409, description: '专辑名称已存在' })
  @UseGuards(AuthGuard('jwt'))
  @Post()
  async createAlbum(
    @Body() createAlbumDto: CreateAlbumDto,
  ): Promise<AlbumResponseDto> {
    return this.albumService.createAlbum(createAlbumDto);
  }

  @ApiOperation({ summary: '获取所有专辑' })
  @ApiResponse({ status: 200, type: PaginationResponseDto<AlbumResponseDto> })
  @Get()
  async getAllAlbums(
    @Query() queryDto: AlbumsPaginationQueryDto,
  ): Promise<PaginationResponseDto<AlbumResponseDto> | AlbumResponseDto[]> {
    // 如果提供了page或limit参数，使用分页方法
    if (queryDto.page !== undefined || queryDto.limit !== undefined) {
      return this.albumService.findAlbumsWithPagination(queryDto);
    }
    // 否则返回所有数据（保持向后兼容）
    return this.albumService.getAllAlbums();
  }

  @ApiOperation({ summary: '获取单个专辑' })
  @ApiResponse({ status: 200, type: AlbumResponseDto })
  @ApiResponse({ status: 404, description: '专辑不存在' })
  @ApiParam({ name: 'id', description: '专辑ID' })
  @Get(':id')
  async getAlbumById(@Param('id') id: number): Promise<AlbumResponseDto> {
    console.log(id, 'getAlbumById');
    return this.albumService.getAlbumById(id);
  }

  @ApiOperation({ summary: '更新专辑' })
  @ApiResponse({ status: 200, type: AlbumResponseDto })
  @ApiResponse({ status: 404, description: '专辑不存在' })
  @ApiResponse({ status: 409, description: '专辑名称已存在' })
  @ApiParam({ name: 'id', description: '专辑ID' })
  @UseGuards(AuthGuard('jwt'))
  @Put(':id')
  async updateAlbum(
    @Param('id') id: number,
    @Body() updateAlbumDto: UpdateAlbumDto,
  ): Promise<AlbumResponseDto> {
    return this.albumService.updateAlbum(id, updateAlbumDto);
  }

  @ApiOperation({ summary: '删除专辑' })
  @ApiResponse({ status: 204, description: '删除成功' })
  @ApiResponse({ status: 404, description: '专辑不存在' })
  @ApiParam({ name: 'id', description: '专辑ID' })
  @UseGuards(AuthGuard('jwt'))
  @HttpCode(204)
  @Delete(':id')
  async deleteAlbum(@Param('id') id: number): Promise<void> {
    return this.albumService.deleteAlbum(id);
  }

  @ApiOperation({ summary: '获取专辑的所有歌曲' })
  @ApiResponse({ status: 200, type: [Song] })
  @ApiResponse({ status: 404, description: '专辑不存在' })
  @ApiParam({ name: 'id', description: '专辑ID' })
  @Get(':id/songs')
  async getSongsByAlbum(@Param('id') id: number): Promise<Song[]> {
    console.log(id, '获取专辑的所有歌曲');
    return this.albumService.getSongsByAlbum(id);
  }

  @ApiOperation({ summary: '获取歌手的所有专辑' })
  @ApiResponse({ status: 200, type: [AlbumResponseDto] })
  @ApiParam({ name: 'singerId', description: '歌手ID' })
  @Get('singer/:singerId')
  async getAlbumsBySinger(
    @Param('singerId') singerId: number,
  ): Promise<AlbumResponseDto[]> {
    return this.albumService.getAlbumsBySinger(singerId);
  }

  @ApiOperation({ summary: '搜索专辑' })
  @ApiResponse({ status: 200, type: [AlbumResponseDto] })
  @ApiQuery({ name: 'keyword', description: '搜索关键词' })
  @Get('search')
  async searchAlbums(
    @Query('keyword') keyword: string,
  ): Promise<AlbumResponseDto[]> {
    return this.albumService.searchAlbums(keyword);
  }
}
